<?php
namespace App\Model;

/**
 * 存入报价详情相关
 * FromTradeDetailModel.php
 * @author zhouqiang
 * @date 2018/05/11
 */
class FromTradeDetailModel extends BaseModel 
{
	protected $table = 'from_trade_detail';

    public static function createTradeDetail($tradeId, $details){
        $model = new self ();
        $items = [ ];

        foreach ( $details as $item ) {
            $items [] = [
                'from_trade_id' =>$tradeId,
                'o_id' => $item ['o_id'],
                'app_id' => $item ['app_id'],
                'bot_id' => $item ['bot_id'],
                'amount' => 1,
                'created_at'  =>  date('Y-m-d H:i:s'),
                'updated_at'  =>  date('Y-m-d H:i:s'),
            ];
        }
        $result = $model->insert($items);
        if (! $result) {
            return false;
        }
        return true;
    }

    public static function updateFromTradeDetailNewAssetId($fromTradeId, $asset)
    {
        $where = array(
            'from_trade_id' => $fromTradeId,
            'o_id' => $asset['o_id'],
        );

        $field = array(
            'new_assetid' => $asset['id'],
            'app_id' => $asset['appid'],
            'context_id' => $asset['contextid'],
            'class_id' => $asset['classid'],
            'instance_id' => $asset['instanceid'],
            'amount' => $asset['amount'],
        );

        return static::where($where)->update($field);
    }
}